package com.cy.ppj.web.front.product;

import com.cy.ppj.comm.utils.JsonObjectMapper;
import com.cy.ppj.comm.utils.SpringELUtils;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.domian.city.City;
import com.cy.ppj.model.enums.ProductAttachmentType;
import com.cy.ppj.model.query.ProductQuery;
import com.cy.ppj.model.vo.ProductDetailVO;
import com.cy.ppj.model.vo.ProductExtendPathVO;
import com.cy.ppj.model.vo.ProductVO;
import com.cy.ppj.service.city.CityService;
import com.cy.ppj.service.product.ProductService;
import com.cy.ppj.service.product.TagService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 前台-产品管理
 * @author: Ocean
 * @create: 2018-07-18 10:01
 **/
@Slf4j
@RestController
@RequestMapping("/product")
@Api(tags = "前台-产品管理")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CityService cityService;

    @ApiOperation("获取前端产品列表")
    @GetMapping("/list")
    public PageBean<ProductVO> list(ProductQuery productQuery) {
        return productService.queryProductList(productQuery);
    }

    @ApiOperation("获取前端产品详情信息")
    @GetMapping("/detail")
    public Result<ProductDetailVO> detail(@ApiParam(value = "产品id", example = "0", required = true) @RequestParam Long productId) {
    	ProductDetailVO productDetailVO = productService.queryProductDetail(productId, this.getUser().getUserId());
    	return ok("查询成功", productDetailVO);
    }
    
    @ApiOperation("获取产品类型")
    @GetMapping("/type/list")
    public Result getProductTypeList() {
    	return ok("查询成功", tagService.queryAllProductTypeTagsInCache());
    }

    @ApiOperation("测评")
    @PostMapping("/assessment")
    public Result assessment(@RequestBody String params, Long productId) {

        Map<String, Object> map = JsonObjectMapper.read(params, Map.class);

        Double amount = SpringELUtils.getValue("", map, Double.class);

        return ok("", amount);
    }

    @ApiOperation("获取前台首页推荐产品列表数据")
    @GetMapping("/home/list")
    public PageBean<ProductVO> homeProductList(ProductQuery productQuery) {
        return productService.homeProducts(productQuery);
    }
    
    @ApiOperation("获取城市列表")
    @GetMapping("/city/list")
    public Result cityList() {
    	List<City> list = cityService.getAllCityInCache();
    	return ok("", list);
    }
    
    @ApiOperation("获取产品推广二维码页面图片地址")
    @GetMapping("/get/extend/qrcode/url")
    public Result getProductExtendQRCodeUrl(@ApiParam(value = "产品ID", example = "0", required = true) @RequestParam Long productId) {
    	ProductExtendPathVO model = productService.selectPathByProductIdAndType(productId, ProductAttachmentType.EXTEND_QR_CODE);
    	Map<String, String> map = new HashMap<>();
    	map.put("path", model.getCodePath());
    	map.put("mobile", this.getUser().getMobile());
    	return ok("获取成功", map);
    }
}
